package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportTaskExecutor;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportTaskExecutorDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2020-05-20 17:34:38
 */
public class TransportTaskExecutorConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportTaskExecutorConverts.class);

    public static List<TransportTaskExecutorDTO> convert(List<TransportTaskExecutor> transportTaskExecutorList) {
        if (CollectionUtils.isEmpty(transportTaskExecutorList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportTaskExecutorDTO> transportTaskExecutorListResult = Lists.newArrayList();
        for (TransportTaskExecutor transportTaskExecutor : transportTaskExecutorList) {
            transportTaskExecutorListResult.add(convert(transportTaskExecutor));
        }
        return transportTaskExecutorListResult;
    }

    public static TransportTaskExecutorDTO convert(TransportTaskExecutor transportTaskExecutor) {
        try {
            return BeanUtils.copy(transportTaskExecutor, TransportTaskExecutorDTO.class);
        } catch (Exception e) {
            LOG.error("TransportTaskExecutor到TransportTaskExecutorDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportTaskExecutor到TransportTaskExecutorDTO转换失败", e);
        }
    }

    public static TransportTaskExecutor convert(TransportTaskExecutorDTO transportTaskExecutorDTO) {
        try {
            return BeanUtils.copy(transportTaskExecutorDTO, TransportTaskExecutor.class);
        } catch (Exception e) {
            LOG.error("TransportTaskExecutorDTO到TransportTaskExecutor转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportTaskExecutorDTO到TransportTaskExecutor转换失败", e);
        }
    }
}